from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from loguru import logger

from app.core.config import settings
from app.tasks import binance_spider_task
from pytz import timezone

from app.core.dingtalk import RobotFactory

jobstores = {
    'default': SQLAlchemyJobStore(url=settings.SQLALCHEMY_DATABASE_URI)  # SQLAlchemyJobStore指定存储链接
}

executors = {
    'default': ThreadPoolExecutor(20)
}

job_defaults = {
    'coalesce': True,
    'misfire_grace_time': None
}

scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone="Asia/Shanghai")
# if settings.ENV != 'dev':
#     scheduler.add_job(binance_spider_task.calendarSpiderJob, 'cron', hour=11)
#     scheduler.add_job(binance_spider_task.instrumentSpiderJob, 'cron', hour=10)
#     scheduler.add_job(binance_spider_task.barSpiderJob, 'cron', hour=9)
#
#     scheduler.add_job(tdx_spider_task.calendarSpiderJob, 'cron', hour=16)
#     scheduler.add_job(tdx_spider_task.instrumentSpiderJob, 'cron', hour=17)
#     scheduler.add_job(tdx_spider_task.barSpiderDayJob, 'cron', hour=18)
#     scheduler.add_job(tdx_spider_task.barSpiderHourJob(), 'cron', hour=21)
#     scheduler.add_job(tdx_spider_task.realtimeSpiderDayJob, 'cron', day_of_week='0-5', hour='9-16', minute='*/2')

# def test():
#     robot = RobotFactory.getAvailable()
#     robot.sendText('定时任务机器人测试')
#     logger.info("定时任务测试")
#
# scheduler.add_job(test, trigger='interval',seconds=5)
